package cn.javasea.patterns;

import cn.javasea.patterns.obv.EventObv;
import cn.javasea.patterns.obv.entity.AlarmBo;
import cn.javasea.patterns.obv.entity.DataBo;
import com.google.common.eventbus.EventBus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *@ClassName EventRegister
 *@Description TODO
 *@Author longxiaonan@163.com
 *@Date 2022/5/7 0007 18:32
 */
@RestController
@RequestMapping
public class EventRegister {

    private EventBus eventBus = new EventBus(); // 同步阻塞模式

    public EventRegister(List<EventObv> eventObvList){
        System.out.println(eventObvList);
        eventObvList.forEach(e -> {
            eventBus.register(e);
        });
    }

    /**
     * http://localhost:8080/test1
     */
    @GetMapping("/test1")
    public void post1(){
        Long userId = (long)(Math.random() * 1000);
        AlarmBo alarmBo = new AlarmBo();
        alarmBo.setName("alarm");
        eventBus.post(alarmBo);
    }

    /**
     * http://localhost:8080/test2
     */
    @GetMapping("/test2")
    public void post2(){
        Long userId = (long)(Math.random() * 1000);
        DataBo dataBo = new DataBo();
        dataBo.setName("alarm");
        eventBus.post(dataBo);
    }

}
